<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <div class="left">
            <i class="el-icon-s-unfold" />
            <span style="font-family:'微软雅黑';font-weight:400;font-size:18px;">数据列表</span>
          </div>
          <div class="right">
            <el-button size="small" @click="handleAdd" style="font: 15px">添加</el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table :data="tableData" border style="width: 100%; text-align: center">
      <el-table-column fixed prop="id" label="编号" width="150" text-align: center></el-table-column>
      <el-table-column prop="name" label="模版名称" width="150"></el-table-column>
      <el-table-column prop="specNum" label="规格数量" width="150"></el-table-column>
      <el-table-column prop="paraNum" label="参数数量" width="150"></el-table-column>
      <el-table-column prop="set" label="设置" width="300">
        <!-- <template slot-scope="scope"> -->
        <el-button @click="formatList" type="text" size="small">规格列表</el-button>
        <el-button @click="paramList" type="text" size="small">参数列表</el-button>
        <!-- </template> -->
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="UpdateType(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteRow(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="total"
       layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 添加和修改表单 -->
    <el-dialog
      :title="form.id == -1 ? '添加类型' : '修改类型'"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="form" :rules="rules" ref="pojoForm" label-width="80px">
        <el-form-item label="类型名称" prop="name">
          <div class="widthInput">
            <el-input v-model="form.name"></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="centerButton">
            <el-button
              type="primary"
              @click="form.id === null ? addData('pojoForm'): updateData('pojoForm')"
            >确定</el-button>
            <el-button @click="resetForm('pojoForm')">重置</el-button>
            <el-button @click="dialogFormVisible = false">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// import AddOrUpdateSP from "./addOrUpdateSP"
// import Page from "@/utils/page.vue"
import templateApi from "@/api/goods/template";
export default {
  // components: { Page },
  data() {
    return {
      total: 0, // 总记录数
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示10条数据,
      dialogFormVisible: false,
      formLabelWidth: "100px",
      list: null, //请求回来填充表格的数据
      page: 1, //默认显示第1页
      limit: 2, //默认一次显示10条数据
      rules: {
        name: [{ required: true, message: "模板名称不能为空", trigger: "blur" }]
      },
      form: {
        id: null,
        name: ""
      },
      tableData: []
    };
  },
  created() {
    this.fetchData();
  },

  methods: {
    // 当每页显示条数改变后,被触发 , val是最新的每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    // 当页码改变后,被触发 , val 是最新的页面
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    //分页查询
    fetchData() {
      templateApi.pageInfo(this.currentPage, this.pageSize).then(response => {
        const resp = response.data;
        console.log(resp);
        // this.tableData = resp;
        this.tableData = resp.data.records;
        this.total = resp.data.total;
      });
    },
    //编辑弹出框
    UpdateType(id) {
      this.handleAdd();
      templateApi.findById(id).then(response => {
        const resp = response.data;
        if (resp.code == 0) {
          this.form = resp.data;
        }
      });
    },
    //编辑方法
    updateData(formName) {
      console.log("updateData");
      this.$refs[formName].validate(valid => {
        if (valid) {
          templateApi
            .updateById(this.form.id, this.form.name)
            .then(response => {
              const resp = response.data;
              if (resp.code == 0) {
                this.fetchData();
                this.dialogFormVisible = false; // 关闭窗口
                this.$message({
                  message: "编辑成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: resp.msg,
                  type: "warning"
                });
              }
            });
        }
      });
    },
    //删除方法
    deleteRow(id) {
      this.$confirm("确认删除这条记录吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      })
        .then(() => {
          // 确认
          console.log("确认");
          templateApi.deleteById(id).then(response => {
            const resp = response.data;
            console.log(resp.code);
            this.$message({
              message: resp.msg,
              type: resp.code == 0 ? "success" : "error"
            });
            if (resp.code == 0) {
              // 删除成功，刷新列表数据
              this.fetchData();
            }
          });
        })
        .catch(() => {
          // 取消，不用理会
          console.log("取消");
        });
    },
    //新增方法
    addData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          templateApi.add(this.form.name).then(response => {
            const resp = response.data;
            if (resp.code == 0) {
              //新增成功，刷新列表数据
              this.fetchData();
              this.dialogFormVisible = false; // 关闭窗口
              this.$message({
                message: "新增成功",
                type: "success"
              });
            } else {
              // 失败，来点提示信息
              this.$message({
                message: resp.msg,
                type: "warning"
              });
            }
          });
        }
      });
    },

    // 弹出新增窗口
    handleAdd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        // this.$nextTick()它是一个异步事件，当渲染结束 之后 ，它的回调函数才会被执行
        // 弹出窗口打开之后 ，需要加载Dom, 就需要花费一点时间，我们就应该等待它加载完dom之后，再进行调用resetFields方法，重置表单和清除样式
        this.$refs["pojoForm"].resetFields();
      });
    },

    // 重置
    resetForm(pojoForm) {
      this.$refs[pojoForm].resetFields();
    },
    //跳转数据
    // getlist() {
    //   var start = (this.page - 1) * this.limit
    //   var end = this.page * this.limit
    //   this.$axios
    //     .get(url + "?start=" + start + "&end=" + end)
    //     .then(response => {
    //       this.list = response.data.items
    //       this.total = response.data.total
    //       resolve()
    //     })
    //     .catch(err => {
    //       reject(err)
    //     })
    // },
    //规格列表
    formatList() {
      console.log();
      this.$router.replace({ path: "/goods/formatList" });
    },
    //参数列表
    paramList() {
      console.log();
      this.$router.push("/goods/paramList");
    }
  }
};
</script>
<style scoped>
.el-table .cell,
.el-table--border td:first-child .cell,
.el-table--border th:first-child .cell {
  text-align: center;
}
.widthInput {
  width: 100%;
}
.centerButtont {
  text-align: center;
}
.el-dialog {
  height: 60%;
}
.el-pagination {
  float: right;
}
a {
  color: black;
  text-decoration: none;
}
a:hover {
  color: blue;
}
.el-row {
  margin-bottom: 20px;
  width: 100%;
}
.el-col {
  border-radius: 4px;
  height: 40px;
}
.bg-purple-dark {
  background: #a5b1c2;
}
.left {
  float: left;
}
.right {
  float: right;
}
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
</style>